package com.ltmonitor.web.controller.mobile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ltmonitor.vo.MobileVehicleItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.Department;
import com.ltmonitor.entity.DriverInfo;
import com.ltmonitor.entity.Terminal;
import com.ltmonitor.entity.VehicleData;
import com.ltmonitor.entity.VehicleInfoModifyRecord;
import com.ltmonitor.service.IDriverInfoService;
import com.ltmonitor.service.IRealDataService;
import com.ltmonitor.service.ITerminalService;
import com.ltmonitor.web.controller.GenericAction;
import com.ltmonitor.web.util.JsonMessage;
import com.ltmonitor.web.util.OnlineUser;
import com.ltmonitor.web.vo.VehicleModel;

@Controller
@RequestMapping("/mobile")
public class MobileVehicleAction extends GenericAction {

	@Autowired
	private ITerminalService terminalService;

	@Autowired
	private IDriverInfoService driverInfoService;


	@Autowired
	private IRealDataService realDataService;
	
	@ResponseBody
	@RequestMapping("/saveVehicle.action")
	public JsonMessage save(MobileVehicleItem vd, Model m, boolean saveAndNew) {
		if (vd.getDepId() == 0) {
			return json(false, "请选择车组");
		}
		
		m.addAttribute("saveAndNew", saveAndNew);

		long vehicleId = vd.getVehicleId();
		String oldSimNo = vd.getSimNo();
		List<VehicleInfoModifyRecord> modifyRecordList = new ArrayList<VehicleInfoModifyRecord>();

		VehicleData v = null;
		if(vehicleId == 0)
		{
			v = new VehicleData();
			v.setSimNo(vd.getSimNo());
			v.setDepId(vd.getDepId());
			v.setPlateColor(vd.getPlateColor());
			v.setPlateNo(vd.getPlateNo());
		}
		else if (vehicleId > 0) {
			OnlineUser user = this.getOnlineUser();
			VehicleData oldVd = this.vehicleDataService.load(vehicleId);
			oldSimNo = oldVd.getSimNo();
			if (oldVd.getPlateNo().equals(vd.getPlateNo()) == false
					|| oldVd.getPlateColor() != vd.getPlateColor()) {
				VehicleInfoModifyRecord vr = new VehicleInfoModifyRecord();
				vr.setVehicleId(vehicleId);
				vr.setType(VehicleInfoModifyRecord.MODIFY_PLATENO);
				vr.setUserName(user.getName());
				StringBuilder detail = new StringBuilder();
				if (oldVd.getPlateNo().equals(vd.getPlateNo()) == false) {
					detail.append("车牌号由[").append(oldVd.getPlateNo())
							.append("]变更为[").append(vd.getPlateNo())
							.append("]");
				}
				if (oldVd.getPlateColor() != vd.getPlateColor()) {
					String oldColorDescr = super.convert(
							"" + oldVd.getPlateColor(), "plateColor");
					String colorDescr = super.convert("" + vd.getPlateColor(),
							"plateColor");
					if (detail.length() > 0)
						detail.append(",");
					detail.append("车牌颜色由[").append(oldColorDescr)
							.append("]变更为[").append(colorDescr).append("]");
				}
				vr.setDetail(detail.toString());
				modifyRecordList.add(vr);
			}
			if (oldVd.getSimNo().equals(vd.getSimNo()) == false) {
				VehicleInfoModifyRecord vr = new VehicleInfoModifyRecord();
				vr.setVehicleId(vehicleId);
				vr.setType(VehicleInfoModifyRecord.MODIFY_SIMNO);
				vr.setUserName(user.getName());
				StringBuilder detail = new StringBuilder();
				detail.append("sim卡号由[").append(oldVd.getSimNo())
						.append("]变更为[").append(vd.getSimNo()).append("]");

				vr.setDetail(detail.toString());
				modifyRecordList.add(vr);
			}
			if (oldVd.getDepId() != vd.getDepId()) {
				VehicleInfoModifyRecord vr = new VehicleInfoModifyRecord();
				vr.setVehicleId(vehicleId);
				vr.setType(VehicleInfoModifyRecord.MODIFY_DEPARTMENT);
				vr.setUserName(user.getName());
				Department oldDep = this.departmentService
						.fetchDepartment(oldVd.getDepId());
				Department dep = this.departmentService.fetchDepartment(vd
						.getDepId());
				StringBuilder detail = new StringBuilder();
				detail.append("过户由[").append(oldDep.getName()).append("]变更为[")
						.append(dep.getName()).append("]");
				vr.setDetail(detail.toString());
				modifyRecordList.add(vr);
			}
			if (oldVd.getTermId() != vd.getTermId()) {
				if (oldVd.getTermId() > 0) {
					try {
						Terminal t = this.terminalService.load(oldVd
								.getTermId());
						t.setBind(false);
						this.terminalService.saveOrUpdate(t);
					} catch (Exception e) {
						log.error(e.getMessage(), e);
					}
				}
			}


			oldVd.setDepId(vd.getDepId());
			oldVd.setPlateColor(vd.getPlateColor());
			oldVd.setPlateNo(vd.getPlateNo());
			oldVd.setSimNo(vd.getSimNo());
			v = oldVd;
		}
		try {
			Terminal t = null;
			if (vd.getTermId() > 0) {
				try {
					t = this.terminalService.load((long)vd.getTermId());
				} catch (Exception e) {
					log.error(e.getMessage(), e);
				}
			} else {
				t = new Terminal();
			}
			if (vd != null) {
				t.setTermNo(vd.getTermNo());
				t.setTermType(vd.getTermType());
				t.setInstallTime(new Date());
				t.setBind(true);
				t.setSimNo(vd.getSimNo());
				terminalService.saveOrUpdateTerminal(t);
			}
			vd.setTermId((int)t.getEntityId());
			vehicleDataService.saveVehicleData(v, modifyRecordList);
			//entityID = "" + ((TenantEntity) entity).getEntityId();
			m.addAttribute("message","保存成功!");

			try {
				this.realDataService.updateVehicleData(oldSimNo, v);
			} catch (Exception ex) {
				log.error(ex.getMessage(), ex);
			}
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			m.addAttribute("message",ex.getMessage());
		}

		return json(true, "保存成功");

	}
}
